import 'dart:convert';

import 'package:xws/helpers/as_t.dart';

/// 系统消息类型
/// * [DEF] 默认（不支持的消息类型）
/// * [FRIEND_ADD] 好友添加
/// * [INTEGRAL_CHANGE] 积分变动
enum SysNotifyType {
  DEF,
  FRIEND_ADD,
  INTEGRAL_CHANGE,
}

const List<String> _msgTypes = <String>[
  'FRIEND_ADD',
  'INTEGRAL_CHANGE',
];

extension ExMT on SysNotifyType {
  String get str => _msgTypes[index];
}

SysNotifyType _getType(String k) {
  switch (k) {
    case 'FRIEND_ADD':
      return SysNotifyType.FRIEND_ADD;
    case 'INTEGRAL_CHANGE':
      return SysNotifyType.INTEGRAL_CHANGE;
    default:
      return SysNotifyType.DEF;
  }
}

/// 系统消息项
class SysNotify {
  SysNotify({
    required this.id,
    required this.conversationRelationId,
    required this.userId,
    required this.content,
    required this.messageType,
    required this.createdAt,
  });

  factory SysNotify.fromJson(Map<String, dynamic> jsonRes) => SysNotify(
        id: asT<String>(jsonRes['id']),
        conversationRelationId: asT<String>(jsonRes['conversationRelationId']),
        userId: asT<String>(jsonRes['userId']),
        content: asT<Map<String, dynamic>>(json.decode(asT<String>(jsonRes['content']))),
        messageType: _getType(asT<String>(jsonRes['messageType'])),
        createdAt: asT<String>(jsonRes['createdAt']),
      );

  late String id;
  late String conversationRelationId;
  late String userId;
  late Map<String, dynamic> content;
  late SysNotifyType messageType;
  late String createdAt;

  String get dateStr {
    try {
      return createdAt.replaceFirst('T', ' ');
    } catch (e) {
      return createdAt;
    }
  }
}
